package com.handong.handserialprogram.module.baiduoverlay;

//import com.baidu.mapapi.map.AMap;
//import com.baidu.mapapi.map.MapStatusUpdateFactory;
//import com.baidu.mapapi.map.Marker;
//import com.baidu.mapapi.map.Overlay;
//import com.baidu.mapapi.map.MarkerOptions;
//import com.baidu.mapapi.model.LatLngBounds;


import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;

import java.util.ArrayList;
import java.util.List;

/**
 * 该类提供一个能够显示和管理多个Overlay的基类
 * Created by userKey on 2017/9/20.
 */

public abstract class OverlayManager  {

    private AMap mAMap;
    private List<MarkerOptions> mMarkerOptionsList;
    private List<Marker> mOverlayList;
    private OnMarkerClick mMarkerClick;

    public OverlayManager(AMap AMap, OnMarkerClick markerClick) {
        this.mAMap = AMap;
        this.mMarkerClick = markerClick;
        mOverlayList = new ArrayList<>();
        mMarkerOptionsList = new ArrayList<>();
    }

    /**
     * 获取需要绘制的路线。
     *
     * @return
     */
    public abstract List<MarkerOptions> getMarkerOptions();

    public final void addToAMap() {
        if (mAMap == null) {
            return;
        }
        removeFromAMap();
        List<MarkerOptions> list = getMarkerOptions();
        if (list != null && !list.isEmpty()) {
            mMarkerOptionsList.addAll(list);
        }
        for (MarkerOptions options : list) {
            mOverlayList.add(mAMap.addMarker(options));
        }
    }

    public final void removeFromAMap() {
        if (mAMap == null) {
            return;
        }
        //for (Overlay marker : mOverlayList) {
        //    marker.remove();
        //}
        mAMap.clear();
        mOverlayList.clear();
        mMarkerOptionsList.clear();
    }

    /**
     * 缩放地图，使所有Overlay都在合适的视野内。
     */
    public void zoomToSpan() {
        if (mAMap == null || mOverlayList == null || mOverlayList.isEmpty()) {
            return;
        }
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for (Marker overlay : mOverlayList) {
            if (overlay instanceof Marker) {
                builder.include(((Marker) overlay).getPosition());
            }
        }
        mAMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(),0));
        //mAMap.setMapStatus(MapStatusUpdateFactory.newLatLngBounds(builder.build()));
    }

    public boolean onOverlayMarkerClick(Marker marker) {
        if (mOverlayList == null || mOverlayList.isEmpty() || mMarkerClick == null) {
            return false;
        }
        int len = mOverlayList.size();
        Marker end = (Marker) mOverlayList.get(len - 2);
        if (marker == end) {
            return mMarkerClick.endMarkerClick(marker);
        }
        Marker start = (Marker) mOverlayList.get(len - 3);
        if (marker == start) {
            return mMarkerClick.startMarkerClick(marker);
        }
        return false;
    }

    public Marker getStartMarker() {
        if (mOverlayList == null || mOverlayList.isEmpty()) {
            return null;
        }
        int len = mOverlayList.size();
        Marker start = (Marker) mOverlayList.get(len - 3);
        return start;
    }

    public void destroy() {
        mOverlayList.clear();
        mMarkerOptionsList.clear();
        mOverlayList = null;
        mMarkerOptionsList = null;
    }

    public interface OnMarkerClick {

        boolean endMarkerClick(Marker marker);

        boolean startMarkerClick(Marker marker);
    }
}
